<template>
  <div class="teacher-list-box">
    <div style="display:flex">
      <!-- <div>
        <el-button style="margin-right:22px" size="mini" type="success" @click="addFormAlert">单个添加</el-button>
      </div>-->
      <div class="search-Box">
        <div class="left-box">
          <div class="item">
            <div class="in-item">
              <span class="label" style>教师编号:</span>
              <el-input size="mini" placeholder="输入搜索编号" v-model="tempSearchParam.nid"></el-input>
            </div>
            <div class="in-item">
              <span class="label" style>所属系别:</span>
              <el-select
                size="mini"
                clearable
                v-model="departmentList.value"
                placeholder="请选择"
                @change="cOptionChange"
              >
                <el-option
                  v-for="item in departmentList.list"
                  :key="item._id"
                  :label="item.name"
                  :value="item._id"
                ></el-option>
              </el-select>
              <!-- <el-input size="mini" placeholder="输入搜索" v-model="tempSearchParam.specialty"></el-input> -->
            </div>
          </div>
        </div>
        <div class="right-box">
          <div class="item">
            <el-button
              style="margin-left:20px"
              size="mini"
              type="primary"
              icon="el-icon-search"
              @click="search"
            >搜索</el-button>
            <el-upload
              class="upload"
              ref="upload"
              action="notUse"
              :file-list="fileList"
              :auto-upload="false"
              :http-request="uploadFile"
              :before-upload="beforeUpload"
              accept=".xls, .xlsx"
              :limit="1"
            >
              <el-button slot="trigger" size="small" type="primary">导入教师列表</el-button>
              <el-button
                style="margin-left: 10px;"
                size="small"
                type="success"
                @click="submitUpload"
              >上传到服务器</el-button>
              <div slot="tip" class="el-upload__tip">
                <i style="font-style:normal;font-weight:bold">提示</i>:导入教师信息，只能上传xls/xlsx文件，且不超过2mb
              </div>
            </el-upload>
          </div>
        </div>
      </div>
    </div>
    <el-table :data="teacherList" stripe style="width: 100%;height:600px">
      <el-table-column prop="department.nid" label="系别编号"></el-table-column>
      <el-table-column prop="department.name" label="系别名称"></el-table-column>
      <el-table-column prop="nid" label="教师编号"></el-table-column>
      <el-table-column prop="name" label="教师姓名"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="checkInfo(scope.$index, scope.row)">查看详细信息</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="foot-box">
      <div style="width:50%">
        <el-button type="success" @click="rufush">刷新</el-button>
      </div>
      <div style="width:50%">
        <el-pagination
          background
          :current-page="option.nowPage"
          layout="prev, pager, next"
          :page-size="option.size"
          :total="option.counts"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import XLSX from "xlsx"; //这个是用来处理表格的一个库
import { handlePageList, readExcel } from "../../plugins";
export default {
  data() {
    return {
      searchParam: {},
      tempSearchParam: {},
      option: { size: 10, counts: 0, nowPage: 1 },
      teacherList: [],
      fileList: [],
      batchPushList: [], //教师批量提交信息，在处理成json，并且处理之后，存进这里
      departmentList: { value: "", list: [] }, //系别列表
      colleageList: [], //分院列表
      isSearch: false
    };
  },
  methods: {
    cOptionChange(e) {
      this.tempSearchParam.department = e;
    },
    cClear() {
      this.tempSearchParam.department = "";
    },
    rufush() {
      // this.option.nowPage = 1;
      this.option.nowPage=1;
      this.searchParam.page = 1;
      this.isSearch = false;
      this.searchParam.department = "";
      this.searchParam.nid = "";
      this.tempSearchParam = {};
      this.departmentList.value = "";
      this.getTeacherList();
    },
    async search() {
      this.option.nowPage = 1;
      this.isSearch = true;
      this.searchParam.page = 1;
      this.searchParam.department = this.tempSearchParam.department;
      this.searchParam.nid = this.tempSearchParam.nid;
      handlePageList(this, "searchList/Teacher", "teacherList");
    },
    async handleCurrentChange(e) {
      this.option.nowPage = e;
      this.searchParam.page = e;
      if (this.isSearch) {
        handlePageList(this, "searchList/Teacher", "teacherList");
      } else {
        handlePageList(this, "list/Teacher", "teacherList");
      }
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    async uploadFile() {
      //这里发送增加请求，增加多数，后端要写个接口
      // TODO:这里添加请求，发送过去的数据就是json格式的batchPushList
      console.log(this.batchPushList);
      const loading = this.$loading({
        lock: true,
        text: "正在上传数据中，请稍等~",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await this.$http.post(
        "insertRest/insertMore/Teacher",
        this.batchPushList
      );
      loading.close();
      if (res.data.success) {
        this.$message.success(res.data.message);
      } else {
        this.$message.error(res.data.message);
      }
      this.getTeacherList();
      this.fileList = [];
    },
    beforeUpload(file) {
      let _this = this;
      //使返回的值变成Promise对象，如果校验不通过，则reject，校验通过，则resolve
      return new Promise(function(resolve, reject) {
        readExcel(_this, file, XLSX, "teacher").then(
          res => {
            const isLt2M = file.size / 1024 / 1204 < 2;
            if (!isLt2M) {
              _this.$message.error("文件大小不能超过2M！");
            }
            if (res && isLt2M) resolve(true);
            reject(false);
          },
          error => {
            //此处为reject返回时候
            _this.$message.error(error);
            reject(false);
          }
        );
      });
    },
    checkInfo(index, row) {
      this.$router.push(`/manager/teacherEdit/${row._id}`);
    },
    handleDelete(index, row) {
      this.$confirm(`是否确定要删除 "${row.name}"`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        const res = await this.$http.delete(`rest/Teacher/${row._id}`);
        this.$message({
          type: "success",
          message: "删除成功!"
        });
        if (this.isSearch) {
          handlePageList(this, "searchList/Teacher", "teacherList");
        } else {
          this.getTeacherList();
        }
      });
    },
    async getDeaprtmentList() {
      let res = await this.$http.get("rest/Department");
      this.departmentList.list = res.data;
    },
    async getCollegeList() {
      let res = await this.$http.get("rest/College");
      this.colleageList = res.data;
    },
    async getTeacherList() {
      handlePageList(this, "list/Teacher", "teacherList");
    }
  },

  created() {
    this.getDeaprtmentList();
    this.getCollegeList();
    this.getTeacherList();
  }
};
</script>

<style scoped>
.upload {
  margin-left: 50px;
}
.search-Box {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  width: 100%;
  /* margin-left: 100px; */
  justify-content: space-between;
}
.left-box {
  width: 50%;
}
.right-box {
  width: 50%;
}
.search-Box .item {
  margin-top: 20px;
  display: flex;
  align-items: center;
}
.search-Box .item:nth-child(1) {
  margin-top: 0;
}
.search-Box .in-item {
  display: flex;
  align-items: center;
}

.search-Box .in-item {
  margin-left: 50px;
}
.search-Box .in-item:nth-child(1) {
  margin: 0;
}
.in-item .label {
  width: 100px;
  font-size: 14px;
  text-align: right;
  margin-right: 10px;
}
.foot-box {
  display: flex;
  margin-top: 20px;
  align-items: flex-end;
  padding: 0 50px;
  padding-right: 80px;
  justify-content: space-between;
}
</style>